package org.luxor.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.luxor.commons.core.validator.group.AddGroup;
import org.luxor.commons.core.validator.group.UpdateGroup;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.commons.jdbc.metadata.PageQuery;
import org.luxor.system.entity.SysUserEntity;
import org.luxor.system.service.ISysDeptHeadService;
import org.luxor.system.service.ISysDeptLeaderService;
import org.luxor.system.service.ISysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark @date 2020-12-29 10:27:12
 */
@RestController
@RequestMapping("/system/sysUser")
@Api(tags = "系统用户")
public class SysUserController extends BaseRestController {

    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISysDeptHeadService sysDeptHeadService;
    @Resource
    private ISysDeptLeaderService sysDeptLeaderService;

    @ApiOperation("分页列表")
    @PostMapping("/list")
    public R<IPage<SysUserEntity>> list(@RequestBody PageQuery<HashMap> searchPage) {
        IPage<SysUserEntity> data = sysUserService.page(searchPage.getPage(), searchPage.getSearch());
        return R.ok(data);
    }

    @ApiOperation("详情信息")
    @ApiImplicitParam(name = "userId", value = "主键ID", paramType = "path", required = true)
    @GetMapping("/info/{userId}")
    public R<SysUserEntity> info(@PathVariable("userId") Long userId) {
        SysUserEntity sysUser = sysUserService.getById(userId);
        return R.ok(sysUser);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public R<SysUserEntity> save(@Validated(AddGroup.class) @RequestBody SysUserEntity sysUser) {
        sysUserService.save(sysUser);
        return R.ok(sysUser);
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public R<SysUserEntity> update(@Validated(UpdateGroup.class) @RequestBody SysUserEntity sysUser) {
        sysUserService.updateById(sysUser);
        return R.ok(sysUser);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        sysUserService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @ApiOperation("查询，统计某部门下的用户总数")
    @GetMapping("/countByDeptId")
    public R<Integer> countByDeptId(@RequestParam Long deptId) {
        int deptUserCount = sysUserService.countByDeptId(deptId);
        return R.ok(deptUserCount);
    }

    @ApiOperation("查询列表，查询某部门下的用户列表")
    @GetMapping("/listByDeptId")
    public R<List<SysUserEntity>> listByDeptId(@RequestParam Long deptId) {
        // 部门成员
        List<SysUserEntity> deptUserList = sysUserService.listByDeptId(deptId);
        // 部门主管
        SysUserEntity deptHead = sysDeptHeadService.getByDeptId(deptId);
        deptUserList.remove(deptHead);
        // 部门分级管理员
        List<SysUserEntity> deptLeaders = sysDeptLeaderService.listByDeptId(deptId);
        deptUserList.removeAll(deptLeaders);

        Map<String, Object> deptUserAllList = new HashMap<>(3);
        deptUserAllList.put("deptHead", deptHead);
        deptUserAllList.put("deptLeaders", deptLeaders);
        deptUserAllList.put("users", deptUserList);
        return R.ok(deptUserList);
    }
}
